﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SecurityDev
{
    public partial class CategoriesForm : Form
    {
        const string fileStr = "Categories";

        public CategoriesForm()
        {
            InitializeComponent();
        }

        private void Category_Load(object sender, EventArgs e)
        {
            if (File.Exists(fileStr))
            {
                StreamReader strR = File.OpenText(fileStr);
                foreach (var line in strR.ReadToEnd().Split('\n'))
                {
                    if (line == "")
                    {
                        break;
                    }
                    categoryListBox.Items.Add(line);
                }
                strR.Close();
            }
            else
            {
                File.Create(fileStr);
            }            
            categoryListBox.SelectedIndex = -1;
        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            categoryListBox.Items.Add(categoryTextBox.Text);
        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {
            var first = from o in DS.outpost._object
                        where o.obj_category == (string)categoryListBox.SelectedItem
                        select o;
            if (first.Count() != 0)
            {
                MessageBox.Show("This item can not be removed because it is used in the database");
                return;
            }
            categoryListBox.Items.RemoveAt(categoryListBox.SelectedIndex);
        }

        private void doneBtn_Click(object sender, EventArgs e)
        {
            StreamWriter strW = null;
            try
            {
                strW = new StreamWriter(File.Open(fileStr, FileMode.Create));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            if (strW != null)
            {
                foreach (var item in categoryListBox.Items)
                {
                    strW.Write(item.ToString()+"\n");
                }
                strW.Close();
            }      
            Close();
        }

        private void categoryListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (categoryListBox.SelectedItems.Count != 0)
            {
                deleteBtn.Enabled = true;
            }
            else
            {
                deleteBtn.Enabled = false;
            }
        }

        private void categoryTB_TextChanged(object sender, EventArgs e)
        {
            if (categoryTextBox.Text != "")
            {
                addBtn.Enabled = true;
            }
            else
            {
                addBtn.Enabled = false;
            }
        }

        private void categoryTextBox_Click(object sender, EventArgs e)
        {
            categoryTextBox.SelectAll();
        }
    }
}
